package com.smartleanx.module.hrm.app.risk;

import cn.hutool.core.collection.CollUtil;
import com.smartleanx.framework.common.enums.CommonStatusEnum;
import com.smartleanx.module.hrm.dal.dataobject.companysystem.CompanySystemDO;
import com.smartleanx.module.hrm.eums.risk.RiskLevelEnum;
import com.smartleanx.module.hrm.eums.risk.RiskTypeEnum;
import com.smartleanx.module.hrm.service.companysystem.CompanySystemService;
import com.smartleanx.module.hrm.service.risk.RiskService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * 规章制度制定是否符合民主程序
 *
 * @author tengcj
 * @since 2024/6/27
 */
@Component
public class RiskDemocraticHandleService implements RiskHandle {

    public static final long RISK_ID = RiskTypeEnum.REGULATION_UNDEMOCRATIC.getCode();

    @Resource
    private RiskService riskService;

    @Resource
    private CompanySystemService companySystemService;

    @Override
    public Long type() {
        return RISK_ID;
    }

    @Override
    public void handleRisk(Set<Long> recordIdList) {
        List<CompanySystemDO> companySystemList = companySystemService.getCompanySystemList(recordIdList, null);
        if (CollUtil.isEmpty(companySystemList)) {
            riskService.deleteRisk(RISK_ID, recordIdList);
            return;
        }
        for (CompanySystemDO companySystemDO : companySystemList) {
            if (Objects.equals(companySystemDO.getComplianceStatus(), CommonStatusEnum.DISABLE.getStatus())) {
                riskService.addRisk(null, RISK_ID, RiskLevelEnum.MAJOR.getLevel(), companySystemDO.getId());
            } else {
                riskService.deleteRisk(null, RISK_ID, companySystemDO.getId());
            }
        }
    }
}
